package com.better.common.Kaptcha;

import com.better.common.base.ServiceResponse;
import com.better.common.jwtauth.secruity.JwtAuthenticationRequest;
import com.better.common.utils.ServiceResponseHandle;
import com.better.common.utils.redis.RedisUtils;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/6.
 */
@Api("获取用户验证码信息")
@Controller
@RequestMapping("/kaptcha")
public class CaptchaController {
    @Autowired
    private Producer captchaProducer;
    @Autowired
    private RedisUtils redisUtils;

    /***
     * 验证码生成
     * ***/
    @ApiOperation(value="获取登录验证码", notes="登录获取用户登录验证码")
    @RequestMapping(value = "/getKaptchaImage",method = RequestMethod.GET)
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //生成验证码
        String token = UUID.randomUUID().toString().replace("-", "");
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalida  te");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        System.out.println("******************生成的验证码是: " + capText + "******************");
        // store the text in the redisUtils
        redisUtils.set(token,capText,60);//设置验证码60秒过期
        Cookie cookie = new Cookie(Constants.KAPTCHA_SESSION_KEY,token);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
    /***
     * 提供给前端验证输入的验证码是否正确
     * ****/
    @ApiOperation(value="验证登录验证码", notes="验证登录验证码")
    @RequestMapping(value = "validCode", method = RequestMethod.POST)
    public ServiceResponse<?> doLogin(@RequestBody JwtAuthenticationRequest authenticationRequest, HttpServletRequest request) {
        if (!authenticationRequest.getValidCode().equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))) {
          return ServiceResponseHandle.failed(null);
        } else {
            return ServiceResponseHandle.success(null);
        }

    }
}
